<?php
// connect to MySQL
$db = mysqli_connect("nikolaswo.fatcowmysql.com", "basics", "basics");

// verify connection with MySQL
if(mysqli_connect_errno()) {
	echo "<h1>MySQL Connection not successful!</h1>";
} else {
	$db->select_db("basics");
}
?>
<html>
<head>
<style type="text/css">
h1 {
	background:blue;
	color:white;
}
table {
	margin-top: 1em;
	border: 1px solid grey;
}
td {
	border: 1px solid grey;
	padding:0.5em;
}

</style>
</head>
<body>
<h1>The World's Simplest Facebook</h1>
<form action="<?php echo $_SERVER["SCRIPT_NAME"]; ?>" method="POST">
Username:<input type="text" name="username"/>
Password:<input type="password" name="password"/>
<input type="submit" value="Login to Facebook!"/>
</form>
<?php
// get user and pass from the POST request
$user = $db->real_escape_string(@$_POST['username']);
$pass = $db->real_escape_string(@$_POST['password']);

// check login is correct
$q = "SELECT * FROM login WHERE USERNAME='" . $user . "' AND PASSWORD='" . $pass . "'";
$result = $db->query($q);

// if login is correct, get profile
if($result->fetch_assoc()) {
	// get profile from database
	$q = "SELECT * FROM facebook WHERE USERNAME='" . $user . "'";
	$result = $db->query($q);
	$r = $result->fetch_assoc();
} else {
	echo "Login is incorrect! Try again!";
	die();
}
?>
<h2><?php echo $r["FIRST_NAME"] . " " . $r["LAST_NAME"]; ?></h2>
<img src="img/<?php echo $r["USERNAME"] . ".jpg"; ?>" width="200px" height="200px"/>
<table>
<tr><td>Contact Email</td><td><?php echo $r["EMAIL"]; ?></td></tr>
<tr><td>Phone Number</td><td><?php echo $r["PHONE"]; ?></td></tr>
<tr><td>Date of Birth</td><td><?php echo $r["DOB"]; ?></td></tr>
<tr><td>Gender</td><td>
<?php
	$g = $r["GENDER"];
	if( $g == "M" ) {
		echo "Male";
	} else if( $g == "F" ) {
		echo "Female";
	}
?>
</td></tr>
<tr><td>Favorite Food</td><td><?php echo $r["FOOD"]; ?></td></tr>
<tr><td>Relationship Status</td>
<td>
<?php
	$rs = $r["RELATIONSHIP_STATUS"];
	if( $rs == "M" ) {
		echo "Married";
	} else if( $rs == "S" ) {
		echo "Single";
	}
?>
</td></tr>

</table>
</body>
</html>